﻿using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ExcelData
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var path = @"D:\MyStudy\NetDemo\ExcelData\Data";
            DirectoryInfo theFolder = new DirectoryInfo(path);
            //获取文件夹下文件
            FileInfo[] files = theFolder.GetFiles();
            foreach (var file in files)
            {
                var name = file.Name.ToLower();
                var fullName = file.FullName;

                ReadCsv(fullName);
            }
        }

        private void ReadCsv(string fullName)
        {
            try
            {

                //实例化一个datatable用来存储数据
                DataTable dt = new DataTable();
                //文件流读取
                System.IO.FileStream fs = new System.IO.FileStream(fullName, System.IO.FileMode.Open);
                System.IO.StreamReader sr = new System.IO.StreamReader(fs, Encoding.GetEncoding("gb2312"));
                string tempText = "";
                while ((tempText = sr.ReadLine()) != null)
                {
                    string[] arr = tempText.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                    if (arr.Length == 11 && arr[0] != "ProductID")
                    {
                        var a1 = Convert.ToDecimal(arr[7]);
                        var a2 = Convert.ToDecimal(arr[8]);
                        var a3 = Convert.ToDecimal(arr[9]);
                        var a4 = Convert.ToDecimal(arr[10]);

                        if (a1 > 0 || a2 > 0 || a3 > 0 || a4 > 0)
                        {
                            LogMethod.log(fullName);
                        }
                    }

                    //一般第一行为标题，所以取出来作为标头
                    //if (isFirst)
                    //{
                    //    foreach (string str in arr)
                    //    {
                    //        dt.Columns.Add(str);
                    //    }
                    //    isFirst = false;
                    //}
                    //else
                    //{
                    //    //从第二行开始添加到datatable数据行
                    //    DataRow dr = dt.NewRow();
                    //    for (int i = 0; i < dt.Columns.Count; i++)
                    //    { dr[i] = i < arr.Length ? arr[i] : ""; }
                    //    dt.Rows.Add(dr);
                    //}
                }
                sr.Close();
                fs.Close();

            }
            catch (Exception ex)
            {
                LogMethod.log(ex.ToString());
            }
        }


        public void ReadFile(string testReportPath)
        {
            Dictionary<string, Dictionary<int, Dictionary<string, object>>> valueMap = new Dictionary<string, Dictionary<int, Dictionary<string, object>>>();

            using (FileStream fs = File.Open(testReportPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                IWorkbook workbook = new HSSFWorkbook(fs);
                ISheet sheet = workbook.GetSheetAt(0);
                IRow row = sheet.GetRow(0);  //读取当前行数据
                                             //LastRowNum 是当前表的总行数-1（注意）
                int offset = 0;

                for (int i = 0; i <= sheet.LastRowNum; i++)
                {
                    row = sheet.GetRow(i);  //读取当前行数据
                    if (row != null)
                    {
                        //LastCellNum 是当前行的总列数
                        if (row.LastCellNum > 9)
                        {
                            for (int j = 7; j < 10; j++)
                            {
                                //读取该行的第j列数据
                                string value = row.GetCell(j).ToString();
                                if (!string.IsNullOrEmpty(value))
                                {
                                    var v = Convert.ToInt32(value);
                                    if (v > 0)
                                    {

                                    }
                                }

                            }
                        }


                    }
                }
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                Convert.ToDecimal("sss");

            }
            catch (Exception ex)
            {
                LogMethod.log("StackTrace：" + ex.StackTrace);
                LogMethod.log("------");
                LogMethod.log("ex：" + ex);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            for (int q = 0; q < 1000000; q++)
            {
                textBox1.Text = q.ToString();
                //  Application.DoEvents();//实时响应文本框中的值
            }
        }


        private Stopwatch m_WatchTimer = new Stopwatch();
        private string m_HisSpan = "";
        private void button4_Click(object sender, EventArgs e)
        {
            m_WatchTimer.Start();
            ShowTime();
        }

        private void ShowTime()
        {
            Thread thread = new Thread(new ThreadStart(_ShowTime));
            thread.Start();
        }

        private void _ShowTime()
        {
            while (true)
            {
                if (this.InvokeRequired)
                {
                    MethodInvoker invoker = delegate ()
                    {
                        TimeSpan newSpan = new TimeSpan(m_WatchTimer.Elapsed.Ticks);
                        lbTotalTime.Text = $"{newSpan.Days}天{newSpan.Hours}小时{newSpan.Minutes}分{newSpan.Seconds}秒";
                    };
                    this.BeginInvoke(invoker);
                    Thread.Sleep(10);
                }
                else
                {
                    lbTotalTime.Text = "close";
                }

            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            m_WatchTimer.Stop();
            ShowTime();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            m_WatchTimer.Restart();
            ShowTime();
        }


        private OpenFileDialog OpenFileDialog1 = new OpenFileDialog();

        private void button8_Click(object sender, EventArgs e)
        {
            string file;
            OpenFileDialog1.Multiselect = true;
            OpenFileDialog1.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory + "Data";
            OpenFileDialog1.Title = "请选择文件";
            OpenFileDialog1.Filter = "所有文件(*xls*)|*.xls*"; //设置要选择的文件的类型
            if (OpenFileDialog1.ShowDialog() == DialogResult.OK)
            {
                file = OpenFileDialog1.FileName;// .FileNames;//返回文件的完整路径   
                ExcelHelper.TestResult(file);
            }
            else
            {
                file = null;
            }
        }
    }
}
